/*
 * 版权所有 (C) 2013 长沙市红耀通信息技术有限公司。保留所有权利。
 *
 * Download http://www.zhiqim.com/fadfox/ 欢迎加盟[知凡狐]兴趣小组。
 *
 * 创建说明： 2013-7-28 由 zhichenggang创建
 */
package net.redxyz.repost.resource.action;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import net.redxyz.repost.resource.SessionUser;
import net.redxyz.repost.service.dao.ItemDao;
import net.redxyz.top.TaobaoService;
import net.redxyz.top.data.item.Item;
import net.redxyz.top.data.shop.SellerCat;
import net.redxyz.top.repost.dbo.AssRepostSchedule;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.ZmlContexts;
import org.zhiqim.httpd.context.annotation.AnIntercept;
import org.zhiqim.httpd.context.core.Action;
import org.zhiqim.kernel.annotation.AnFilterNot;
import org.zhiqim.kernel.extend.HashMapSO;
import org.zhiqim.kernel.extend.MapSO;
import org.zhiqim.kernel.paging.PageResult;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Selector;

/**
 * 宝贝选择器
 *
 * @version v7.0.1 @author zouzhigang 2021-7-30 新建与整理
 */
public class SelectorAction implements Action
{

    public void execute(HttpRequest request) throws Exception
    {
        SessionUser user = request.getSessionUser(SessionUser.class);
        String sessionId = user.getSessionId();
        String nick = user.getNick();
        List<SellerCat> sellerCats = TaobaoService.getShopService().getSellercatsList(nick, sessionId);

        request.setAttribute("sellerCats", sellerCats);
    }
    
    /**
     * 根据条件查询宝贝信息
     *
     * @param request 请求
     * @param action 请求功能
     * @param sellerCid 分类
     * @param q 标题关键字
     * @param isOnsale 是否选择出售中
     * @param isInventory 是否选择仓库中
     * @return 返回显示内容
     * @exception 异常
     */
    @AnIntercept("chkAjax")
    @AnFilterNot
    public static String doQuery(HttpRequest request, String action, String sellerCid, String q, boolean isOnsale, boolean isInventory, int page)
        throws Exception
    {
        SessionUser sessionUser = request.getSessionUser(SessionUser.class);
        if (sessionUser == null)
            return "您登录已超时，请重新登录...";

        String nick = sessionUser.getNick();
        String sessionId = sessionUser.getSessionId();

        // 查询指定上下架计划宝贝
        List<AssRepostSchedule> repostList = ORM.table().list(AssRepostSchedule.class, new Selector("nick", nick));

        // 过滤
        List<Long> filterIidList = new ArrayList<Long>();
        filterItemIidList(filterIidList, repostList);

        int pageSize = 10;
        if (page < 1)
            page = 1;

        PageResult<Item> result = ItemDao.getItemPage(nick, sessionId, sellerCid, q, isOnsale, isInventory, filterIidList, page, pageSize);
        @SuppressWarnings("unchecked")
        LinkedHashMap<Long,Item> itemMap = (LinkedHashMap<Long,Item>) sessionUser.getValue("items_" + action);

        MapSO varMap = new HashMapSO();
        varMap.put("result", result);
        varMap.put("q", q);
        varMap.put("itemMap", itemMap);
        varMap.put("selectNum", itemMap == null ? 0 : itemMap.size());

        return ZmlContexts.parseZmlPath(request, "/zview/selectorInfo.zml", varMap);
    }

    /**
     * 过滤加入自定义上下计划
     */
    private static void filterItemIidList(List<Long> filterIidList, List<AssRepostSchedule> detailList)
    {
        for (AssRepostSchedule detail : detailList)
        {
            filterIidList.add(detail.getNumIid());
        }
    }
    
    /**
     * 选中一个宝贝
     *
     * @param request 请求
     * @param action 请求功能
     * @param numIid 宝贝编号
     * @param title 宝贝标题
     * @param picUrl 宝贝地址
     * @param delistTime 下架时间
     */
    @SuppressWarnings("unchecked")
    @AnIntercept("chkAjax")
    @AnFilterNot
    public static void doSelectItem(HttpRequest request, String action, long numIid, String title, String picUrl, String delistTime)
    {
        SessionUser sessionUser = request.getSessionUser(SessionUser.class);
        if (sessionUser == null)
        {
            request.setResponseResult(1, "您登录已超时，请重新登录...");
            return;
        }
        
        LinkedHashMap<Long, Item> itemMap = (LinkedHashMap<Long, Item>) sessionUser.getValue("items_" + action);
        
        if (itemMap == null)
        {
            itemMap = new LinkedHashMap<Long, Item>();
            sessionUser.setValue("items_" + action, itemMap);
        }

        Item item = new Item();
        item.setNumIid(numIid);
        item.setTitle(title);
        item.setPicUrl(picUrl);
        item.setDelistTime(delistTime);
        itemMap.put(numIid, item);
    }

    /**
     * 取消一个宝贝选择
     *
     * @param request 请求
     * @param action 请求功能
     * @param numIid 宝贝编号
     */
    @SuppressWarnings("unchecked")
    @AnIntercept("chkAjax")
    public static void doCancelItem(HttpRequest request, String action, long numIid)
    {
        SessionUser sessionUser = request.getSessionUser(SessionUser.class);
        if (sessionUser == null)
        {
            request.setResponseResult(1, "您登录已超时，请重新登录...");
            return;
        }

        LinkedHashMap<Long,Item> itemMap = (LinkedHashMap<Long,Item>) sessionUser.getValue("items_" + action);
        if (itemMap == null)
            return;

        itemMap.remove(numIid);
    }
}
